Reverse each word in a sentenceΒΆ
[word[::-1] for word in LOW]
Given a long sentence, reverse each word of the sentence individually in the sentence itself.
Input:
Geeks For Geeks is good to learn
Expected output:
skeeG roF skeeG si doog ot nrael
Input:
Split Reverse Join
Output:
tilpS esreveR nioJ
def reverse_word_sentence(S):
# 1. split the sentence into list of words
LOW = S.split(" ")
# 2. reverse each word and create a new list of words
LOW1 = [word[::-1] for word in LOW]
# 3. join the new list of words
S1 = " ".join(LOW1)
# All in One line
# return ' '.join(word[::-1] for word in S.split(" "))
return S1
# test
sentence = "GeeksforGeeks is good to learn"
print(reverse_word_sentence(sentence))
Output:
skeeGrofskeeG si doog ot nrael